package com.weishuang.monitor.leader.controller;


import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import com.weishuang.monitor.leader.entity.SysMenu;
import com.weishuang.monitor.leader.entity.dto.MenuDTO;
import com.weishuang.monitor.leader.entity.vo.MenuVO;
import com.weishuang.monitor.leader.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/leader/sysMenu")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取树形结构数据(不获取功能)
     * @param menu 筛选信息
     * @return
     */
    @GetMapping(value = "getTreeMenuList")
    public Result getTreeMenuList(MenuDTO menu){
        List<MenuVO> menuList = menuService.getTreeMenuList(menu);
        return ResultUtils.success(menuList);
    }

    /**
     * 获取菜单列表
     * @param menu 筛选信息
     * @return
     */
    @GetMapping(value = "getMenuList")
    public Result getMenuList(MenuDTO menu){
        List<MenuVO> menuList = menuService.getMenuList(menu);
        return ResultUtils.success(menuList);
    }

    /**
     * 获取当前用户的菜单列表
     * @return
     */
    @GetMapping(value = "getTreeMenuByUser")
    public Result getTreeMenuByUser(){
        List<MenuVO> menus = menuService.getMenuListByUserId();
        return ResultUtils.success(menus);
    }

    /***
     * 新增菜单
     * @param menu 菜单信息
     * @return
     */
    @PostMapping
    public Result addMenu(@Validated SysMenu menu){
        int i = menuService.addMenu(menu);
        return ResultUtils.success(i);
    }

    /**
     * 修改菜单
     * @param id 菜单ID
     * @param menu 菜单信息
     * @return
     */
    @PutMapping("/{id}")
    public Result updateMenu(@PathVariable String id, @Validated SysMenu menu){
        menu.setId(id);
        menu.updateInit();
        int i = menuService.updateMenu(menu);
        return ResultUtils.success(i);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteMenu(@PathVariable String id) {
        int i = menuService.deleteMenu(id);
        return ResultUtils.success(i);
    }

    /**
     * 查询当前角色所拥有的菜单：分配角色菜单
     * @param roleId 角色ID
     * @return
     */
    @GetMapping(value = "listMenuTreeByRoleId/{roleId}")
    public Result listMenuTreeByRoleId(@PathVariable String roleId){
        List<MenuVO> menus = menuService.listMenuTreeByRoleId(roleId);
        return ResultUtils.success(menus);
    }
}
